package hoemWork;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.Scanner;

public class Reflection {

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		String name;
		System.out.println("请输入类的全路径名：");
		name = new Scanner(System.in).next();

		try {
			Class cl = Class.forName(name);
			Class superc1 = cl.getSuperclass();
			String modifiers = Modifier.toString(cl.getModifiers());

			if (modifiers.length() > 0) {
				System.out.print(modifiers + " ");
			}
			System.out.print("class " + cl.getSimpleName());
			if (superc1 != null) {
				System.out.print(" extends " + superc1.getSimpleName());
			}

			Class interfaces[] = cl.getInterfaces();
			if (interfaces != null && interfaces.length > 0) {
				System.out.print(" implements ");
				for (int i = 0; i < interfaces.length; i++) {
					System.out.print(interfaces[i].getSimpleName());
					if (i < interfaces.length - 1) {
						System.out.print(", ");
					}
				}
			}
			System.out.println("\n {");

			printFields(cl);// 获得字段
			System.out.println();
			printConstructors(cl);// 获得构造函数
			System.out.println();
			printMethods(cl);// 获得方法
			System.out.println("}");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		System.exit(0);
	}

	private static void printConstructors(Class cl) {
		Constructor[] construtor = cl.getDeclaredConstructors();
		if (construtor != null && construtor.length > 0) {
			for (Constructor cons : construtor) {
				String modifiers = Modifier.toString(cons.getModifiers());
				String name = cl.getSimpleName();
				System.out.print("    ");
				System.out.print(modifiers + " " + name + "(");

				Parameter[] paramTypes = cons.getParameters();
				printParamType(paramTypes);

				System.out.println(")");
			}
		}
	}

	/**
	 * 处理参数，组装一个参数的字符串
	 */
	private static void printParamType(Parameter[] paramTypes) {
		if (paramTypes != null && paramTypes.length > 0) {
			for (int i = 0; i < paramTypes.length; i++) {
				System.out.print(paramTypes[i].getType().getSimpleName());
				System.out.print(" " + paramTypes[i].getName());
				if (i < paramTypes.length - 1) {
					System.out.print(", ");
				}
			}
		}
	}

	private static void printMethods(Class cl) {

		Method[] methods = cl.getDeclaredMethods();
		for (Method method : methods) {
			String name = method.getName();
			String modifier = Modifier.toString(method.getModifiers());
			System.out.print("    ");
			if (modifier.length() > 0) {
				System.out.print(modifier + " ");
			}
			Class returnType = method.getReturnType();
			if (returnType.isArray()) {
				System.out.print(returnType.getComponentType().getSimpleName() + "[]" + " " + name + "(");
			} else {
				System.out.print(returnType.getSimpleName() + " " + name + "(");
			}

			Parameter[] paramTypes = method.getParameters();
			printParamType(paramTypes);
			System.out.println(")");
		}

	}

	private static void printFields(Class cl) {
		Field[] fields = cl.getDeclaredFields();
		for (Field field : fields) {
			String name = field.getName();
			Class type = field.getType();
			String modifier = Modifier.toString(field.getModifiers());
			System.out.print("    ");
			if (modifier.length() > 0) {
				System.out.print(modifier + " ");
			}
			System.out.println(type.getSimpleName() + " " + name + ";");
		}
	}
}
